import 'package:flutter/material.dart';
import 'package:hello_word/common/helper/resource_helper.dart';
import 'package:hello_word/common/manager/router_manager.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with TickerProviderStateMixin {
  static const int countdownCount = 6;
  final int countdownTime = (countdownCount ~/ 2).toInt();

  AnimationController _logoController;
  Animation<double> _animation;
  AnimationController _countdownController;

  @override
  void initState() {
    print("initState");
    _logoController = AnimationController(
        vsync: this, duration: Duration(milliseconds: 1500));
    _animation = Tween(begin: 0.0, end: 1.0).animate(
        CurvedAnimation(curve: Curves.easeInOutBack, parent: _logoController));
    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        // print("监听到完成：开始返回操作");
        _logoController.reverse();
      } else if (status == AnimationStatus.dismissed) {
        // print("监听到结束：开始重复操作");
        _logoController.forward();
      }
    });
    _logoController.forward();

    _countdownController = AnimationController(
        vsync: this, duration: Duration(seconds: countdownTime));
    _countdownController.forward();
    super.initState();
  }

  @override
  void dispose() {
    print("dispose");
    _logoController.dispose();
    _countdownController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print("build");
    return Scaffold(
      body: WillPopScope(
          child: Stack(
            fit: StackFit.expand,
            children: <Widget>[
              Image.asset(
                ImageHelper.wrapAssets(
                    Theme.of(context).brightness == Brightness.light
                        ? "splash_bg.png"
                        : "splash_bg_dark.png"),
                fit: BoxFit.fill,
              ),
              _AnimatedLogo(
                animation: _animation,
              ),
              Align(
                alignment: Alignment(0.0, 0.7),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    _AnimatedScrollLogo(
                      animation: _animation,
                    ),
                  ],
                ),
              ),
              Align(
                alignment: Alignment.bottomRight,
                child: SafeArea(
                  child: InkWell(
                    onTap: () {
                      print("skip click action");
                      animatedCompletedAction(context);
                    },
                    child: Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 5),
                      margin: EdgeInsets.only(right: 20, bottom: 20),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(40),
                        color: Colors.black.withAlpha(100),
                      ),
                      child: _AnimatedCountdown(
                        context: context,
                        animation: StepTween(begin: countdownCount, end: 0)
                            .animate(_countdownController),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          onWillPop: () => Future.value(false)),
    );
  }
}

class _AnimatedCountdown extends AnimatedWidget {
  final Animation<int> animation;

  _AnimatedCountdown({Key key, this.animation, context})
      : super(key: key, listenable: animation) {
    this.animation.addListener(() {
      // print("监听倒计时：${animation.status}");
      if (this.animation.status == AnimationStatus.completed) {
        animatedCompletedAction(context);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var value = animation.value + 1;
    return Text(
      (value == 0 ? "" : "$value | ") + "splashSkip",
      style: TextStyle(color: Colors.white),
    );
  }
}

class _AnimatedLogo extends AnimatedWidget {
  _AnimatedLogo({Key key, Animation<double> animation})
      : super(key: key, listenable: animation) {
    animation.addListener(() {
      // print(animation.value);
    });
  }

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return AnimatedAlign(
      alignment: Alignment(0, 0.2 + animation.value * 0.3),
      duration: Duration(milliseconds: 10),
      curve: Curves.bounceOut,
      child: Image.asset(
        ImageHelper.wrapAssets('splash_flutter.png'),
        width: 280,
        height: 120,
      ),
    );
  }
}

class _AnimatedScrollLogo extends AnimatedWidget {
  _AnimatedScrollLogo({Key key, Animation<double> animation})
      : super(key: key, listenable: animation) {
    animation.addListener(() {
      // print(animation.value);
    });
  }
  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Image.asset(
          ImageHelper.wrapAssets("splash_fun.png"),
          width: 140 * animation.value,
          height: 80 * animation.value,
        ),
        Image.asset(
          ImageHelper.wrapAssets("splash_android.png"),
          width: 200 * (1 - animation.value),
          height: 80 * (1 - animation.value),
        )
      ],
    );
  }
}

void animatedCompletedAction(context) {
  Navigator.of(context).pushReplacementNamed(RouteName.tab);
}
